#include "database.h"
#include "maintancefiles/emithub.h"
/*CREATE  TABLE "main"."horde"
 *("id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "item" TEXT, "bid" TEXT, "buyout" TEXT,
 *"quantity" TEXT, "timeLeft" TEXT, "rand" TEXT, "seed" TEXT)*/
DataBase* DataBase::p_instance = 0;
DataBase::DataBase(QObject *parent) :
    QObject(parent)
{   
    dbase = QSqlDatabase::addDatabase("QSQLITE");
    dbase.setDatabaseName(":memory:");
    if (!dbase.open()) {
        emit myError( "DataBase.err = base not up" );
    }
    QSqlQuery query;
    query.exec("CREATE  TABLE \"main\".\"horde\" ( \"item\" TEXT, \"bid\" TEXT, \"buyout\" TEXT, \"quantity\" TEXT, \"timeLeft\" TEXT, \"rand\" TEXT, \"seed\" TEXT)");

}

DataBase::~DataBase()
{
    dbase.close();
}

void DataBase::insertDump2Base( const QList<QString>& _s )
{
    qDebug()<<"insert to DB";
    int _l = _s.length()-7;
    QSqlQuery query;
    int c=0;
    while( c <= _l)
    {
        query.prepare("INSERT INTO 'horde' ('item','bid','buyout','quantity','timeLeft','rand','seed') "
                      "VALUES (:item,:bid,:buyout,:quantity,:timeLeft,:rand,:seed)");
        query.bindValue(":item", _s[0+c]);
        query.bindValue(":bid", _s[1+c]);
        query.bindValue(":buyout", _s[2+c]);
        query.bindValue(":quantity", _s[3+c]);
        query.bindValue(":timeLeft", _s[4+c]);
        query.bindValue(":rand", _s[5+c]);
        query.bindValue(":seed", _s[6+c]);
        if ( !query.exec() )
            emit myError(query.lastError().text());
        query.clear();
        c = c+ 7;
    }
    qDebug()<<"insert to DB done";
    emit baseReady();
}

QList<QMap<QString,QString>> DataBase::_proposalInCollection(const QList<QString>*_l)
//auto DataBase::_proposalInCollection(const QList<QString>*_l)
{
    QStringList lst;
    lst = *_l;

    QList<QMap<QString,QString>> res;
    QMap<QString,QString> s;
    QSqlQuery query;
    QString _sq;
    query.setForwardOnly(1);
    /* foreach (QString _v, lst)
    {
        _sq = "SELECT * FROM horde WHERE item = '"+_v+"'";
        if (!query.exec(_sq))
            emit myError(query.lastError().text());
        else
        {
            if(!query.first())
            {
                s.insert("item",       _v);
                s.insert("bid",     "n/a");
                s.insert("buyout",  "n/a");
                s.insert("quantity","n/a");
                s.insert("timeLeft","n/a");
                s.insert("rand",    "n/a");
                s.insert("seed",    "n/a");
                res.append(s);
                s.clear();

            }
            else{
                do
                {
                    s.insert("item",    query.value(0).toString());
                    s.insert("bid",     query.value(1).toString());
                    s.insert("buyout",  query.value(2).toString());
                    s.insert("quantity",query.value(3).toString());
                    s.insert("timeLeft",query.value(4).toString());
                    s.insert("rand",    query.value(5).toString());
                    s.insert("seed",    query.value(6).toString());
                    res.append(s);
                    s.clear();
                }
                while ( query.next() );
            }
        }
    }*/
    foreach (QString _v, lst)
    {
        _sq = "SELECT * FROM horde WHERE item = '"+_v+"'";
        if (!query.exec(_sq))
            emit myError(query.lastError().text());
        else
        {
            if(!query.first())
            {
                s.insert("item",       _v);
                s.insert("bid",     "n/a");
                s.insert("buyout",  "n/a");
                s.insert("quantity","n/a");
                s.insert("timeLeft","");
                s.insert("rand",    "");
                s.insert("seed",    "");
                res.append(s);
                s.clear();

            }
            else{
                QString _s = query.value(0).toString();
                QString _bi,_bu;
                long bid = 0;
                long maxBid = 0;
                long minBid = 100000000;
                long buyout = 0;
                long maxBuyout = 0;
                long minBuyout = 100000000;
                long quantity = 0;
                long x,y,z;
                do
                {
                    x = query.value(1).toInt();
                    y = query.value(2).toInt();
                    z = query.value(3).toInt();
                    bid += x;
                    buyout += y;
                    quantity += z;
                    maxBid = (maxBid<x/z)?(int)x/z:maxBid;
                    minBid = (minBid>x/z)?(int)x/z:minBid;
                    maxBuyout = (maxBuyout<y/z)?(int)y/z:maxBuyout;
                    minBuyout = (minBuyout>y/z)?(int)y/z:minBuyout;

                }
                while ( query.next() );
                _bi = QString::number((int)bid/quantity)+"*"+QString::number(minBid)+"*"+QString::number(maxBid);//надо не надо
                _bu = QString::number((int)buyout/quantity)+"*"+QString::number(minBuyout)+"*"+QString::number(maxBuyout);
                s.insert("item",    _s);
                s.insert("bid",     _bi);
                s.insert("buyout",  _bu);
                s.insert("quantity",QString::number(quantity));
                s.insert("timeLeft","");
                s.insert("rand",    "");
                s.insert("seed",    "");
                res.append(s);
                s.clear();
            }
        }
    }
    return res;
}
